import re
import argparse

#  print_output_info:    0.0% |                                                  | 0.01212 samples/s/p  1069 days, 11:17:12 }


def process_log_file(log_file_path):
    max_value = float("-inf")
    min_value = float("inf")
    total_sum = 0
    count = 0
    # 0.01212 samples/s/p
    pattern = r"(\d+\.\d+)( samples/s/p)"
    first_match_skipped = False
    with open(log_file_path, "r") as file:
        for line in file:
            match = re.search(pattern, line)
            if match:
                if not first_match_skipped:
                    first_match_skipped = True
                    continue
                value = float(match.group(1))
                min_value = min(value, min_value)
                max_value = max(value, max_value)
                total_sum += value
                count += 1
    average_value = total_sum / count if count > 0 else 0
    return max_value, average_value, min_value


def calc_performance(value, device_num, tokens_per_sample):
    seconds_per_day = 60 * 60 * 24
    return value * device_num * tokens_per_sample * seconds_per_day / 1000000000

def main(log_path, device_num, tokens_per_sample):
    max_value, average_value, min_value = process_log_file(log_path)
    max_value = calc_performance(max_value, device_num, tokens_per_sample)
    average_value = calc_performance(average_value, device_num, tokens_per_sample)
    min_value = calc_performance(min_value, device_num, tokens_per_sample)
    print(f"unit conversion as(B tokens/day) : max:{max_value:.2f}, min:{min_value:.2f}, ave:{average_value:.2f}")



if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="telechat parse time")
    parser.add_argument(
        "--log_path",
        default=None,
        required=True,
        help="please input log file path, log file should end with .log",
    )
    parser.add_argument(
        "--device_num",
        default=512,
        required=False,
        help="please input device num",
    )
    parser.add_argument(
        "--tokens_per_sample",
        default=8192,
        required=False,
        help="please input seq length",
    )
    args = parser.parse_args()
    main(args.log_path, int(args.device_num), int(args.tokens_per_sample))